package com.ws.hw_student.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ws.hw_student.system.entity.SysUser;
import com.ws.hw_student.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/system/sysUser")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @ApiOperation("分页查询用户列表")
    @GetMapping("/pageRole")
    public Map<String, Object> pageRole(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size,
            @RequestParam(required = false) String userName) {

        Page<SysUser> page = new Page<>(current, size);
        Page<SysUser> userPage = sysUserService.page(page, new QueryWrapper<SysUser>()
                .like(userName != null, "user_name", userName));

        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", userPage);
        return result;
    }

    @ApiOperation("添加用户")
    @PostMapping("/save")
    public Map<String, Object> save(@RequestBody SysUser sysUser) {
        Map<String, Object> result = new HashMap<>();
        boolean success = sysUserService.save(sysUser);
        result.put("success", success);
        if (!success) {
            result.put("message", "添加用户失败");
        }
        return result;
    }

    @ApiOperation("根据ID获取用户详情")
    @GetMapping("/getById")
    public Map<String, Object> getById(@RequestParam Long userId) {
        Map<String, Object> result = new HashMap<>();
        SysUser user = sysUserService.getById(userId);
        if (user != null) {
            result.put("success", true);
            result.put("data", user);
        } else {
            result.put("success", false);
            result.put("message", "用户不存在");
        }
        return result;
    }

    @ApiOperation("更新用户信息")
    @PostMapping("/update")
    public Map<String, Object> update(@RequestBody SysUser sysUser) {
        Map<String, Object> result = new HashMap<>();
        boolean success = sysUserService.updateById(sysUser);
        result.put("success", success);
        if (!success) {
            result.put("message", "更新用户失败");
        }
        return result;
    }

    @ApiOperation("删除用户")
    @PostMapping("/delete")
    public Map<String, Object> delete(@RequestParam Long userId) {
        Map<String, Object> result = new HashMap<>();
        boolean success = sysUserService.removeById(userId);
        result.put("success", success);
        if (!success) {
            result.put("message", "删除用户失败");
        }
        return result;
    }

    @ApiOperation("用户登录")
    @GetMapping("/login")
    public Map<String, Object> login(@ModelAttribute SysUser sysUser) {
        Map<String, Object> result = new HashMap<>();
        SysUser user = sysUserService.findByName(sysUser.getUserName());
        if (user == null) {
            result.put("success", false);
            result.put("message", "用户名不存在");
        } else if (!user.getPassword().equals(sysUser.getPassword())) {
            result.put("success", false);
            result.put("message", "密码错误");
        } else {
            result.put("success", true);
            result.put("data", user);
        }
        return result;
    }
}